﻿@inherits AdminCompontentBase
<DefaultSheetDialog @bind-Value="_visible" Title="@T("Claim")">
    <MForm>
        <AutoHeight Overflow>
            <AutoHeightContent>
                <MRow Class="py-4">
                    <MCol Md="6">
                        <SAutoComplete @bind-Value="@_claimKey" Required
                                       Label="@T("Type")"
                                       Items="@_claims"
                                       ItemText="@(u => $"{u.Name}({u.Description})")"
                                       ItemValue="u => u.Name">
                        </SAutoComplete>
                    </MCol>
                    <MCol Md="6">
                        <STextField Label="@T("Value")" @bind-Value="@_claimValue" Required />
                    </MCol>
                    <MCol Md="12">
                        <SButton Block OnClick="AddClaim">
                            <SIcon Left>
                                @IconConstants.Add
                            </SIcon>
                            @T("Add Claim")
                        </SButton>
                    </MCol>
                    @foreach (var _claimValue in _claimValues)
                    {
                        <MCol Md="12">
                            <STextField Value="@_claimValue.Value" ValueChanged="v=> ValueChanged(_claimValue.Key,v)" TValue="string" Class="claim-value">
                                <PrependInnerContent>
                                    <span>
                                        @_claimValue.Key
                                    </span>
                                    @if (_claims.Any(x => x.Name == _claimValue.Key))
                                    {
                                        <span>
                                            (@(_claims.Find(x => x.Name == _claimValue.Key)?.Description))
                                        </span>
                                    }
                                </PrependInnerContent>
                                <AppendOuterContent>
                                    <MButton OnClick="()=>RemoveItem(_claimValue.Key)">
                                        <SIcon Color="error">@IconConstants.Remove</SIcon>
                                    </MButton>
                                </AppendOuterContent>
                            </STextField>
                        </MCol>
                    }
                </MRow>
            </AutoHeightContent>
            <FooterContent>
                <div class="d-flex mt-6">
                    <MSpacer></MSpacer>
                    <SButton Medium OnClick="Save">@T("Save")</SButton>
                </div>
            </FooterContent>
        </AutoHeight>
    </MForm>
</DefaultSheetDialog>

@code {

    private List<UserClaimSelectDto> _claims = new ();
    private string _claimKey = "", _claimValue = "";
    private Dictionary<string, string> _claimValues = new();
    private bool _visible;
    private Guid _userId;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            _claims = await AuthCaller.UserClaimService.GetSelectAsync();
            StateHasChanged();
        }
        await base.OnAfterRenderAsync(firstRender);
    }

    public async Task ShowAsync(Guid userId)
    {
        _userId = userId;
        _claimValues = await AuthCaller.UserService.GetClaimValuesAsync(_userId);
        _visible = true;
        StateHasChanged();
    }

    void ValueChanged(string k,string v)
    {
        _claimValues[k] = v;
    }

    public void RemoveItem(string key)
    {
        _claimValues.Remove(key);
    }

    async Task Save()
    {
        await AuthCaller.UserService.SaveClaimValuesAsync(new UserClaimValuesDto
        {
            UserId = _userId,
            ClaimValues = _claimValues
        });
        Hidden();
    }

    void Cancel()
    {
        Hidden();
    }

    void Hidden()
    {
        _visible = false;
        _claimKey = "";
        _claimValue = "";
        _claimValues = new();
    }

    async void AddClaim()
    {
        if (_claimKey.IsNullOrEmpty() || _claimValue.IsNullOrEmpty())
        {
            return;
        }
        if (_claimValues.ContainsKey(_claimKey))
        {
            await PopupService.EnqueueSnackbarAsync(T("RepeatAdd"), AlertTypes.Warning);
        }
        _claimValues.TryAdd(_claimKey,_claimValue);
        _claimKey = "";
        _claimValue = "";
    }
}
